package cn.kgc.mushroomcommon.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class UploadUtil {
    public static void uploadSingleFile(
            String endpoint,
            String accessKeyId,
            String accessKeySecret,
            String fileName,
            String folderName,
            String bucketName,
            InputStream inputStream) {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        // String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        //String accessKeyId = "LTAI4Fp685QDUvh2bomEH5te";
        //String accessKeySecret = "nU0bAkjE0WgqWXVKo5Mnk9HVqnIUiE";
        //String bucketName="distribution-crowd-parent";
        //String folderName="project/head";
        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 上传文件流。
            String objectName = folderName + "/" + fileName;
            ossClient.putObject(bucketName, objectName, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 生成文件名
     * @param originalFileName
     * @return
     */
    public static  String generaterFileName(String originalFileName){
        return UUID.randomUUID().toString().replaceAll("-","")+originalFileName;
    }

    public  static String generateDayFolderName(){
        return new SimpleDateFormat("yyyyMMdd").format(new Date());
    }

}
